/*
 * @lc app=leetcode.cn id=173 lang=cpp
 * @lcpr version=30204
 *
 * [173] 二叉搜索树迭代器
 */


// @lcpr-template-start
using namespace std;
#include <algorithm>
#include <array>
#include <bitset>
#include <climits>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <queue>
#include <stack>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
// @lcpr-template-end
// @lc code=start
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class BSTIterator {
public:
    BSTIterator(TreeNode* root) {
        TreeNode* cur = root;
        while(cur) {
            st.push(cur);
            cur = cur->left;
        }
    }
    
    int next() {
        if (!st.empty()) {
            TreeNode* cur = st.top();
            st.pop();
            int val = cur->val;
            if(cur->right) {
                cur = cur->right;
                while(cur) {
                    st.push(cur);
                    cur = cur->left;
                }
            }
            
                
            return val;
        }
        return -1;
    }
    
    bool hasNext() {
        return !st.empty();
    }
private:
    stack<TreeNode*> st;
};

/**
 * Your BSTIterator object will be instantiated and called as such:
 * BSTIterator* obj = new BSTIterator(root);
 * int param_1 = obj->next();
 * bool param_2 = obj->hasNext();
 */
// @lc code=end



